<?php
namespace Tbf\Component\DateTime;
/**
* 一段时间
* 默认包括两头
*/
class Period
{
    protected $start;
    protected $end;
    public function __construct(DateTime $start,DateTime $end)
    {
        $this->start = $start;
        $this->end = $end;
    }
    public function getStart()
    {
        return $this->start;
    }
    public function getEnd()
    {
        return $this->end;
    }
    public function toMysqlArray()
    {
        return array($this->start->toMysql(),$this->end->toMysql());
    }
    /**
    * 转化为unix timestamp
    */
    public function toUnixArray()
    {
        return array($this->start->toUnix(),$this->end->toUnix());
    }
    /**
    * 今天这一段时间
    */
    public static function today()
    {
        return self::fromDate(new DateTime());
    }
    /**
    * 某天的一段时间
    */
    public static function fromDate(DateTime $date)
    {
        $start = $date->dayStart();
        $end = $date->dayEnd();

        return new static($start,$end);
    }
    /**
    * 所有时间
    * 2038问题..
    */
    public static function all()
    {
        $start = '1970-01-02 00:00:00';
        $end = '2037-12-31 23:59:59';

        return self::fromMysql($start,$end);
    }
    /**
    *
    */
    public static function fromMysql($start,$end)
    {
        $start = DateTime::fromMysql($start);
        $end = DateTime::fromMysql($end);

        return new static($start,$end);
    }
}
